<template>

  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>Email:</span>
        <el-input v-model="listQuery.email" clearable placeholder="请输入Email" @keyup.enter="handleFilter" />
      </div>
      <div class="filter-item">
        <span>主题:</span>
        <el-input v-model="listQuery.subject" clearable placeholder="请输入主题" @keyup.enter="handleFilter" />
      </div>
      <div class="filter-item">
        <span>发送状态:</span>
        <el-select v-model="listQuery.status" clearable placeholder="请选择发送状态">
          <el-option label="全部状态" value="" />
          <el-option
            v-for="item in statusOptions"
            :key="'filter_status'+ item.label "
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <el-button class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>

    </div>
    <div class="operationList">
      <div>
        <el-button :loading="sendEmailLoading" class="filter-item" type="primary" icon="Document" @click="handleRetrySendEmail">邮件重发</el-button>
        <el-button text type="primary" icon="Plus" @click="handleSendEmail">发送邮件</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :row-class-name="tableRowClass"
      :data="list"
      fit
      border
      highlight-current-row
      tyle="width: 100%"
      header-cell-class-name="header-cell"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" />
      <el-table-column label="序号" width="100" type="index" :index="calIndex"/>
      <el-table-column min-width="150" label="Email">
        <template #default="scope">
          <span>{{ scope.row.email }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="200" label="发送主题">
        <template #default="scope">
          <span>{{ scope.row.subject }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="80" label="发送状态">
        <template #default="scope">
          <span>{{ statusFilter(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" label="重试次数">
        <template #default="scope">
          <span>{{ scope.row.tryNum }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="100" label="返回消息">
        <template #default="scope">
          <span>{{ scope.row.msg }}</span>
        </template>
      </el-table-column>
      <el-table-column min-width="160" label="响应时间">
        <template #default="scope">
          <span>{{ scope.row.responseDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template #default="scope">
          <el-button size="small" type="danger" plain icon="Delete" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <sendlogForm ref="sendlogForm" @refresh-list="getList"/>
  </div>
</template>

<script>

import Pagination from '@/components/Pagination/index.vue'
import { deleteSendlog, fetchSendLogList, retrySend } from '@/api/email/sendlog'
import sendlogForm from './sendlogForm.vue'

export default {
  name: 'SendLog',
  components: { Pagination, sendlogForm },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      templateList: undefined,
      template: {},
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        subject: undefined,
        email: undefined,
        status: undefined
      },
      showReviewer: false,
      multipleSelection: [],
      sendEmailLoading: false,
      statusOptions: [
        { label: '发送失败', value: '-1' },
        { label: '发送中', value: '0' },
        { label: '发送成功', value: '1' }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {

    /**
     * 获取列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchSendLogList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 搜索列表
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 根据状态搜索
     * @param status 状态
     */
    statusFilter(status) {
      const statusMap = {
        '-1': '发送失败',
        0: '发送中',
        1: '发送成功'
      }
      return statusMap[status + '']
    },
    handleSendEmail() {
      this.$refs.sendlogForm.handleSendEmail()
    },
    /**
     *
     * @param val 模板id
     */
    // handleSelect(val) {
    //   this.templateList.forEach(item => {
    //     if (item.code === val) {
    //       this.template = item
    //       // console.log('this.template', this.template)
    //       this.template.templateContent = unescape(this.template.templateContent)
    //     }
    //   })
    // },
    /**
     * 修改状态
     * @param row 当前列表选中数据
     * @param status 状态
     */
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },

    /**
     * 删除
     * @param row 当前列表选中数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSendlog([row.id]).then(response => {
          if (response.data.success) {
            row.$delAnim = true
            this.getList(false)
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    /**
     * 勾选列表数据
     * @param val 选中数据
     */
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /**
     * 重发
     */
    handleRetrySendEmail() {
      if (this.multipleSelection.length) {
        this.sendEmailLoading = true
        const list = this.multipleSelection
        const ids = []
        list.forEach(function(value) {
          ids.push(value.id)
        })
        const idsStr = ids.join(',')
        retrySend(idsStr).then(() => {
          this.$message.success('提交成功')
          this.$refs.multipleTable.clearSelection()
          this.sendEmailLoading = false
        }).catch(() => {
          this.sendEmailLoading = false
        })
      } else {
        this.$message({
          message: '至少选择一条重发',
          type: 'warning'
        })
      }
    }
  }
}
</script>

